//
//  UIView+JwHolder.m
//  JwPack
//
//  Created by 陈警卫 on 2024-08-22.
//

#import "UIView+JwHolder.h"
#import <objc/runtime.h>
#import "JwMacro.h"
#import "JwCate.h"
#import "JwSingle.h"

NSString *GV_UIViewJwHolder_holdClassName = @"JwHoldView";

NSString *GV_JwHoldView_holdImgName = @"jw_hold_blank";
NSString *GV_JwHoldView_textColorString = @"AFAFAF";
NSString *GV_JwHoldView_btnColorString = @"AFAFAF";

@implementation JwHoldView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    
    self.iconImgV = [[UIImageView alloc] init];
    self.iconImgV.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:self.iconImgV];
    [self.iconImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.centerY.mas_equalTo(self).multipliedBy(0.5);
        make.height.mas_equalTo(self.mas_width).multipliedBy(0.4);
        make.width.mas_equalTo(self.mas_width).multipliedBy(0.4);
    }];
    
    self.tipL = [[UILabel alloc] init];
    self.tipL.textAlignment = NSTextAlignmentCenter;
    self.tipL.font = [UIFont systemFontOfSize:16 weight:(UIFontWeightRegular)];
    self.tipL.textColor = kJwColorHexString(GV_JwHoldView_textColorString);
    self.tipL.numberOfLines = 0;
    [self addSubview:self.tipL];
    [self.tipL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self).multipliedBy(0.8);
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.iconImgV.mas_bottom).offset(15);
    }];
    
    self.sureB = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.sureB.titleLabel.font = [UIFont systemFontOfSize:16 weight:(UIFontWeightRegular)];
    [self.sureB setTitleColor:kJwColorHexString(GV_JwHoldView_btnColorString) forState:(UIControlStateNormal)];
    [self addSubview:self.sureB];
    [self.sureB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(40);
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.tipL.mas_bottom).offset(15);
    }];
    self.sureB.layer.cornerRadius = 5;
    self.sureB.layer.borderWidth = 1.0;
    self.sureB.layer.borderColor = kJwColorHexString(GV_JwHoldView_btnColorString).CGColor;
    
    kJwWeak(self);
    [self.sureB jw_addTargetAction:^(UIButton *button) {
        if (wself.didSureBAction) {
            wself.didSureBAction();
        }
    }];
}

- (void)setupValue:(id)value {
    
}

- (void)setupDataWithImageName:(NSString *)imageName text:(NSString *)text btnText:(NSString *)btnText {
    
    if (imageName) {
        self.iconImgV.hidden = NO;
        self.iconImgV.image = [UIImage jw_imageSkinWithName:imageName];
    } else {
        self.iconImgV.hidden = YES;
        [self.iconImgV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }
    
    if (text) {
        self.tipL.hidden = NO;
        self.tipL.text = text;
    } else {
        self.tipL.hidden = YES;
        [self.tipL mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }
    
    if (btnText) {
        self.sureB.hidden = NO;
        [self.sureB setTitle:kJwLocalizedString(btnText) forState:(UIControlStateNormal)];
    } else {
        self.sureB.hidden = YES;
    }
}

@end

@implementation UIView (JwHolder)

- (JwHoldView *)jw_holdView {
    return objc_getAssociatedObject(self, @selector(jw_holdView));
}

- (void)setJw_holdView:(JwHoldView *)jw_holdView {
    objc_setAssociatedObject(self, @selector(jw_holdView), jw_holdView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)jw_setupHoldViewWithImageName:(NSString *)imageName text:(NSString *)text btnText:(NSString *)btnText {
    if ([self.subviews containsObject:self.jw_holdView]) {
        [self.jw_holdView removeFromSuperview];
        self.jw_holdView = nil;
    }
    Class holdClass = NSClassFromString(GV_UIViewJwHolder_holdClassName);
    self.jw_holdView = [[holdClass alloc] init];
    self.jw_holdView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.jw_holdView];
    [self.jw_holdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.offset(0);
    }];
    
    [self.jw_holdView setupDataWithImageName:imageName text:text btnText:btnText];
    
}

- (void)jw_removeHoldView {
    if (self.jw_holdView) {
        [self.jw_holdView removeFromSuperview];
    }
}

- (void)jw_setupHoldViewWithImageName:(NSString *)imageName text:(NSString *)text {
    [self jw_setupHoldViewWithImageName:imageName text:text btnText:nil];
}

- (void)jw_setupHoldViewWithImageName:(NSString *)imageName btnText:(NSString *)btnText {
    [self jw_setupHoldViewWithImageName:imageName text:nil btnText:btnText];
}

- (void)jw_setupHoldView {
    [self jw_setupHoldViewWithImageName:GV_JwHoldView_holdImgName text:nil btnText:nil];
}

- (void)jw_setupHoldViewWithText:(NSString *)text {
    [self jw_setupHoldViewWithImageName:GV_JwHoldView_holdImgName text:text btnText:nil];
}

- (void)jw_setupHoldViewWithBtnText:(NSString *)btnText {
    [self jw_setupHoldViewWithImageName:GV_JwHoldView_holdImgName text:nil btnText:btnText];
}

- (void)jw_setupHoldViewWithText:(NSString *)text btnText:(NSString *)btnText {
    [self jw_setupHoldViewWithImageName:GV_JwHoldView_holdImgName text:text btnText:btnText];
}



@end
